package com.gitee.qdbp.general.common.biz.sms.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.general.common.api.sms.model.ISmsTemplate;
import com.gitee.qdbp.general.common.api.sms.service.ISmsService;
import com.gitee.qdbp.general.common.biz.sms.utils.DefSmsBusiness;
import com.gitee.qdbp.general.common.biz.sms.utils.ISmsBaseBusiness;
import com.gitee.qdbp.tools.cache.ICacheService;
import com.gitee.qdbp.tools.utils.Config;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 基础短信服务接口
 *
 * @author zhaohuihua
 * @version 160831
 */
public abstract class BaseSmsService implements ISmsService {

    private static final Logger log = LoggerFactory.getLogger(BaseSmsService.class);

    /** 配置文件的路径 **/
    protected String properties;

    protected Config config;

    @Autowired
    protected ICacheService cache;

    protected ISmsBaseBusiness utils;

    public BaseSmsService() {
    }

    public void init() {
        if (VerifyTools.isBlank(properties)) {
            log.info("BaseSmsService.init() properties path is null.");
            config = new Config();
        } else {
            config = new Config(properties);
        }
        if (utils == null) {
            setBaseBusiness(new DefSmsBusiness());
        }
    }

    public void setBaseBusiness(ISmsBaseBusiness utils) {
        this.utils = utils;
        if (utils instanceof Config.Aware) {
            ((Config.Aware) utils).setConfig(config);
        }
        if (utils instanceof ICacheService.Aware) {
            ((ICacheService.Aware) utils).setCacheService(cache);
        }
    }

    public void setCacheService(ICacheService cache) {
        this.cache = cache;
        if (utils instanceof ICacheService.Aware) {
            ((ICacheService.Aware) utils).setCacheService(cache);
        }
    }

    /**
     * 获取验证码的缓存时间配置(单位:秒)
     *
     * @param template 模板类型
     * @return
     * @throws ServiceException
     */
    @Override
    public int getCacheTime(ISmsTemplate template) throws ServiceException {
        return utils.getCacheTime(template.name());
    }

}
